package com.magicstone.common.pagination.service;

public class SQLServerDialect extends Dialect {

	static int getAfterSelectInsertPoint(String sql) {
		int selectIndex = sql.toLowerCase().indexOf( "select" );
		final int selectDistinctIndex = sql.toLowerCase().indexOf( "select distinct" );
		return selectIndex + ( selectDistinctIndex == selectIndex ? 15 : 6 );
	}

	@Override
	public String getLimitString(String sql, int offset, int limit) {
		return new StringBuffer( sql.length() + 8 )
		.append( sql )
		.insert( getAfterSelectInsertPoint( sql ), " top " + limit )
		.toString();
	}

}
